package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.guards.R;
import com.mingtu.guards.bean.DealProcessBean;

import java.util.List;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class DealRecordAdapter extends RecyclerView.Adapter<DealRecordAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<DealProcessBean.DealDataBean> mData;
    private OnItemClickListener mOnItemClickListener = null;

    public DealRecordAdapter(List<DealProcessBean.DealDataBean> data) {
        this.mData = data;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_deal_record, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {

            DealProcessBean.DealDataBean dealDataBean = mData.get(position);
            String time = dealDataBean.getDealTime();
            String info = dealDataBean.getDealInfo();
            holder.tvTime.setText(time + "");
            holder.tvText.setText(info + "");

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void upData(List<DealProcessBean.DealDataBean> data) {
        mData = data;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View view;
        private final TextView tvTime;
        private final TextView tvText;

        public ActivityHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvText = (TextView) view.findViewById(R.id.tv_text);
        }
    }

}
